﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractDLL;
using MimiDouban.Abstract.Beans;
using doubanloginDll8;

namespace MimiDouban
{
	/// <summary>
	/// 分享
	/// </summary>
	public class Share
	{
		/// <summary>
		/// 分享网站
		/// </summary>
		public enum Sites
		{
			/// <summary>
			/// 无分享网站，仅复制网址
			/// </summary>
			None,
			/// <summary>
			/// 豆瓣
			/// </summary>
			Douban,
			/// <summary>
			/// 新浪微博
			/// </summary>
			Weibo,
			/// <summary>
			/// MSN
			/// </summary>
			Msn,
			/// <summary>
			/// 开心网
			/// </summary>
			Kaixin,
			/// <summary>
			/// 人人网
			/// </summary>
			Renren,
			/// <summary>
			/// 腾讯微博
			/// </summary>
			TencentWeibo,
			/// <summary>
			/// 饭否
			/// </summary>
			Fanfou,
			/// <summary>
			/// Facebook
			/// </summary>
			Facebook,
			/// <summary>
			/// Twitter
			/// </summary>
			Twitter
		}

		public static readonly Dictionary<Sites, string> SiteName;

		public DoubanSong Song { get; private set; }

		public DoubanChannel Channel { get; private set; }

		public Sites Site { get; set; }

		public string Text { get; set; }

		public string Url
		{
			get
			{
                return "http://douban.fm/?start=" + Song.Sid + "g" + Song.Ssid + "g" + Channel.ChannelId + "&cid=" + Channel.ChannelId;
			}
		}

		static Share()
		{
			SiteName = new Dictionary<Sites, string>();
			SiteName.Add(Sites.Douban, "豆瓣");
			SiteName.Add(Sites.Facebook, "Facebook");
			SiteName.Add(Sites.Fanfou, "饭否");
			SiteName.Add(Sites.Kaixin, "开心网");
			SiteName.Add(Sites.Msn, "MSN");
			SiteName.Add(Sites.Renren, "人人网");
			SiteName.Add(Sites.TencentWeibo, "腾讯微博");
			SiteName.Add(Sites.Twitter, "Twitter");
			SiteName.Add(Sites.Weibo, "新浪微博");
			SiteName.Add(Sites.None, "*复制网址到剪贴板*");
		}

		public Share(DoubanSong song, DoubanChannel channel, Sites site)
		{
			if (song == null)
				throw new ArgumentNullException("song");
			Song = song;
			Site = site;
            Channel = channel;

			Text = GetShareText(song);
		}

        //public Share(Player player, Sites site)
        //    : this(player.CurrentSong, player.CurrentChannel, player.CurrentDjCate, site)
        //{ }

        //public Share(DoubanSong song, DoubanChannel channel, Cate cate)
        //    : this(song, channel, cate, Sites.None)
        //{ }

        public Share(DoubanSong song, DoubanChannel channel)
            : this(song, channel, Sites.None)
        { }

        //public Share(Player player)
        //    : this(player, Sites.None)
        //{ }

		/// <summary>
		/// 获取分享链接
		/// </summary>
		public string GetShareLink()
		{
			if (Site == null)
				throw new Exception("没有设定分享网站。");
			Parameters parameters = new Parameters(true);
			Parameters tempParas = new Parameters(true);
			string url = null;
			string tempurl = null;
			
			switch (Site)
			{
				case Sites.None:
					throw new InvalidOperationException("复制网址模式不能获取分享链接");
					break;
				case Sites.Douban:
					parameters.Add("name", Song.Title);
                    parameters.Add("href", Url);
					parameters.Add("image", Song.Picture);
					parameters.Add("text", "");
                    parameters.Add("desc", "（来自豆瓣电台客户端【http://code.google.com/p/mimi-douban/】）");
					parameters.Add("apikey", "079055d6a0d5ddf816b10183e28199e8");
					url = ConnectionBase.ConstructUrlWithParameters("http://shuo.douban.com/!service/share", parameters);
					break;
				case Sites.Weibo:
					tempParas.Add("appkey", "3163308509");
                    tempParas.Add("url", Url);
					tempParas.Add("title", Text);
					tempParas.Add("source", "");
					tempParas.Add("sourceurl", "");
					tempParas.Add("content", "utf-8");
					tempParas.Add("pic", Song.Picture);
					tempurl = ConnectionBase.ConstructUrlWithParameters("http://v.t.sina.com.cn/share/share.php", tempParas);
					parameters.Add("type", "redir");
					parameters.Add("vendor", "bshare_sina");
					parameters.Add("url", tempurl);
					url = ConnectionBase.ConstructUrlWithParameters("http://www.douban.com/link2", parameters);
					break;
				case Sites.Msn:
                    tempParas.Add("url", Song.Url);
					tempParas.Add("title", Text);
                    tempParas.Add("screenshot", Song.Picture);
					tempurl = ConnectionBase.ConstructUrlWithParameters("http://profile.live.com/badge", tempParas);
					parameters.Add("type", "redir");
					parameters.Add("vendor", "bshare_msn");
					parameters.Add("url", tempurl);
					url = ConnectionBase.ConstructUrlWithParameters("http://www.douban.com/link2", parameters);
					break;
				case Sites.Kaixin:
					tempParas.Add("rurl", Song.Url);
					tempParas.Add("rcontent", "");
					tempParas.Add("rtitle", Text);
					tempurl = ConnectionBase.ConstructUrlWithParameters("http://www.kaixin001.com/repaste/bshare.php", tempParas);
					parameters.Add("type", "redir");
					parameters.Add("vendor", "bshare_kx");
					parameters.Add("url", tempurl);
					url = ConnectionBase.ConstructUrlWithParameters("http://www.douban.com/link2", parameters);
					break;
				case Sites.Renren:
                    tempParas.Add("url", Url);
					tempParas.Add("title", Text);
					tempurl = ConnectionBase.ConstructUrlWithParameters("http://www.connect.renren.com/share/sharer", tempParas);
					parameters.Add("type", "redir");
					parameters.Add("vendor", "bshare_renren");
					parameters.Add("url", tempurl);
					url = ConnectionBase.ConstructUrlWithParameters("http://www.douban.com/link2", parameters);
					break;
				case Sites.TencentWeibo:
                    parameters.Add("url", Url);
					parameters.Add("title", Text);
					parameters.Add("site", "http://www.kfstorm.com/doubanfm");
					parameters.Add("pic", Song.Picture);
					url = ConnectionBase.ConstructUrlWithParameters("http://v.t.qq.com/share/share.php", parameters);
					break;
				case Sites.Fanfou:
					string fanfouParameters = "?u=" + Uri.EscapeDataString(Song.Url)
						+ "?t=" + Uri.EscapeDataString(Text)
						+ "?d=" + Uri.EscapeDataString("")
						+ "?s=bm";
					url = "http://fanfou.com/share" + "r" + fanfouParameters;
					break;
				case Sites.Facebook:
                    parameters.Add("u", Song.Url);
					parameters.Add("t", Text);
					url = ConnectionBase.ConstructUrlWithParameters("http://www.facebook.com/sharer.php", parameters);
					break;
				case Sites.Twitter:
                    parameters.Add("status", Text + " " + Song.Url);
					url = ConnectionBase.ConstructUrlWithParameters("http://twitter.com/home", parameters);
					break;
				default:
					break;
			}
			return url;
		}

		/// <summary>
		/// 打开分享链接或复制网址
		/// </summary>
		public void Go()
		{
			if (Site == Sites.None)
			{
				try
				{
					System.Windows.Clipboard.Clear();
					System.Windows.Clipboard.SetText(Url);
				}
				catch { }
			}
			else
			{
				System.Diagnostics.Process.Start(GetShareLink());
			}
		}

		/// <summary>
		/// 获取分享文字
		/// </summary>
		static string GetShareText(DoubanSong song)
        {
            return "我正在收听歌曲《" + song.Title + "》（来自 依依-豆瓣电台客户端【http://code.google.com/p/mimi-douban/】）";
            //switch (type)
            //{
            //    case "movie-ost":
            //        return "我正在收听《" + artistName + "》的电影原声 “" + songName + "” （来自豆瓣电台客户端【http://kfstorm.com/doubanfm】 - " + channelName + "）";
            //    case "easy":
            //        return "我正在收听 " + artistName + " 的乐曲《" + songName + "》（来自豆瓣电台客户端【http://kfstorm.com/doubanfm】 - " + channelName + "）";
            //    case "dj":
            //        return "我正在收听节目 《" + songName + "》- " + channelName + "（来自豆瓣电台客户端【http://kfstorm.com/doubanfm】 - DJ兆赫 ）";
            //    default:
            //        return "我正在收听 " + artistName + " 的单曲《" + songName + "》（来自豆瓣电台客户端【http://kfstorm.com/doubanfm】 - " + channelName + "）";
            //}
		}
	}
}
